package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @author Mosetian
     * @date 2022-9-19 10:43
     * @param	shoppingCart
     * @return R<ShoppingCart>
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){

        return shoppingCartService.saveWith(shoppingCart);
    }

    /**
     * 查询购物车
     * @author Mosetian
     * @date 2022-9-19 10:48
     * @param
     * @return R<List<ShoppingCart>>
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, UserHolder.getUserId());

        List<ShoppingCart> list = shoppingCartService.list(qw);

        return R.success(list);
    }

    /**
     * 取消购物车
     * @author Mosetian
     * @date 2022-9-19 18:52
     * @param	shoppingCart
     * @return R<ShoppingCart>
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){

        return shoppingCartService.subShoppingCart(shoppingCart);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, UserHolder.getUserId());

        boolean remove = shoppingCartService.remove(qw);

        return remove ? R.success("清空购物车成功") : R.error("清空购物车失败");
    }
}
